package com.honey.mybatis.generator;

import org.java.plugin.Plugin;
import org.java.plugin.PluginLifecycleException;

import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.ObjectFactory;
import com.honey.generator.java.GeneratorJavaClassPlugin;
import com.honey.generator.java.GeneratorPresentationLayerCompilation;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.mybatis.generator.dao.method.CountMethod;
import com.honey.mybatis.generator.dao.method.DeleteByDynamicMethod;
import com.honey.mybatis.generator.dao.method.DeleteByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.ExistsMethod;
import com.honey.mybatis.generator.dao.method.InsertMethod;
import com.honey.mybatis.generator.dao.method.InsertSelectiveMethod;
import com.honey.mybatis.generator.dao.method.SaveMethod;
import com.honey.mybatis.generator.dao.method.SelectAllMethod;
import com.honey.mybatis.generator.dao.method.SelectByDynamicMethod;
import com.honey.mybatis.generator.dao.method.SelectByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.SelectOneByDynamicMethod;
import com.honey.mybatis.generator.dao.method.UpdateByDynamicMethod;
import com.honey.mybatis.generator.dao.method.UpdateByDynamicSelectiveMethod;
import com.honey.mybatis.generator.dao.method.UpdateByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.UpdateByPrimaryKeySelectiveMethod;

/**
 * mybatis 在mvc三层模型中处在持久层中.
 * @author Administrator
 *
 */
public class MybatisPresentationLayer extends  GeneratorPresentationLayerCompilation {
	
	private MybatisPlugin mybatisPlugin =null;
	
	private GeneratorJavaClassPlugin generatorJavaClassPlugin = null;
	
	private MybatisBaseDaoImplementsExtension mybatisBaseDaoImplementsExtension;
	
	private MybatisBaseDaoInterfaceExtension mybatisBaseDaoInterfaceExtension;
	
	private MybatisDaoImplementsExtension mybatisDaoImplementsExtension;
	
	private MybatisDaoInterfaceExtension mybatisDaoInterfaceExtension;
	
	private MybatisBlobEntityExtension mybatisBlobEntityExtension;
	
	private MybatisEntityExtension mybatisEntityExtension;
	
	private MybatisConditionExtension mybatisConditionExtension;
	
	private MybatisPageExtension mybatisPageExtension;
	
	private MybatisGeneratedCriteriaExtension mybatisGeneratedCriteriaExtension;
	
	private MybatisModelExtension mybatisModelExtension;
	
	public MybatisPresentationLayer(Plugin belongPlugin) {
		super(belongPlugin);
		try {
			 generatorJavaClassPlugin = ( GeneratorJavaClassPlugin )ObjectFactory.getRequestPlugin(belongPlugin, "honey.generator.java.plugin");
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
		mybatisPlugin = (MybatisPlugin)belongPlugin;
		
		this.mybatisBaseDaoImplementsExtension = new MybatisBaseDaoImplementsExtension( belongPlugin );
		this.mybatisBaseDaoInterfaceExtension = new MybatisBaseDaoInterfaceExtension( belongPlugin );
		this.mybatisDaoImplementsExtension = new MybatisDaoImplementsExtension( belongPlugin );
		this.mybatisDaoInterfaceExtension = new MybatisDaoInterfaceExtension( belongPlugin );
		this.mybatisBlobEntityExtension = new MybatisBlobEntityExtension( belongPlugin );
		this.mybatisEntityExtension = new MybatisEntityExtension( belongPlugin );
		this.mybatisConditionExtension = new MybatisConditionExtension( belongPlugin );
		this.mybatisPageExtension = new MybatisPageExtension( belongPlugin );
		this.mybatisGeneratedCriteriaExtension = new MybatisGeneratedCriteriaExtension( belongPlugin );
		this.mybatisModelExtension = new MybatisModelExtension( belongPlugin );
	}
	
	@Override
	public Method[] layerPublicMethod() {
		IntrospectSchema introspectSchema = getIntrospectSchema() ;
		IntrospectSchemaColumn introspectSchemaColumn = getIntrospectSchemaColumn();
		return new Method[]{
			new SelectAllMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new SelectAllMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn,false,true ).createMethod(),
			
			new CountMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new ExistsMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new SelectByDynamicMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new SelectByDynamicMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn,false,true ).createMethod(),
			
			new SelectByPrimaryKeyMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new SelectOneByDynamicMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new UpdateByDynamicMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new UpdateByDynamicSelectiveMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new UpdateByPrimaryKeyMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new UpdateByPrimaryKeySelectiveMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new SaveMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new InsertSelectiveMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new InsertMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new DeleteByPrimaryKeyMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
			new DeleteByDynamicMethod(null,mybatisPlugin,introspectSchema,introspectSchemaColumn ).createMethod(),
		};
	}
	
	@Override
	public FullyQualifiedJavaType layerType() {
		final IntrospectSchema introspectSchema = getIntrospectSchema() ;
		return new AbstractMybatis(mybatisPlugin){
			public FullyQualifiedJavaType getLayerType(){
				return super.getInterfaceClassType(introspectSchema.getSchema(), null);
			}
		}.getLayerType();
	}
	
	@Override
	public GeneratorJavaClass[] getGeneratorJavaClass() {
		return new GeneratorJavaClass[]{
				this.mybatisBaseDaoImplementsExtension,
				this.mybatisBaseDaoInterfaceExtension,
				this.mybatisDaoImplementsExtension,
				this.mybatisDaoInterfaceExtension,
				this.mybatisBlobEntityExtension,
				this.mybatisEntityExtension,
				this.mybatisConditionExtension,
				this.mybatisPageExtension,
				this.mybatisGeneratedCriteriaExtension,
				this.mybatisModelExtension,
		};
	}

	@Override
	public FullyQualifiedJavaType[] requestClass() {
		return new FullyQualifiedJavaType[]{ 
			this.mybatisEntityExtension.getType(),
			this.mybatisModelExtension.getType(),
			this.mybatisConditionExtension.getType(),
			this.mybatisPageExtension.getType(),
		};
	}
	
	@Override
	public GeneratorJavaClassPlugin getGeneratorJavaClassPlugin() {
		return generatorJavaClassPlugin;
	}
}
